40823222 cd2021

  • Home
    • Site Map
    • reveal
    • blog
  • About
  • W1
  • stage1-bg22
    • W2-W4
      • w1 小組討論
      • w2 繪製圖檔
      • w3 程式模擬
      • w4 小組報告
  • stage2-bg10
    • W5
    • W6-W9
      • 零件圖
      • 模擬
      • 影片錄製
      • 參考資料
  • stage3-bg3
    • Task 1
    • Task 2
    • RoboDK
    • 學習python
    • W16
    • stage3模擬
    • w18
  • 直播影片
Task 1 << Previous Next >> RoboDK

Task 2

使用api鍵盤控制。

import sim as vrep
import math
import random
import time
import keyboard
 
print ('Start')
 
# Close eventual old connections
vrep.simxFinish(-1)
# Connect to V-REP remote server
clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)
  
if clientID !=-1:
    print ('Connected to remote API server')
   
    res = vrep.simxAddStatusbarMessage(
        clientID, "40823222",
        vrep.simx_opmode_oneshot)
          
    if res not in (vrep.simx_return_ok, vrep.simx_return_novalue_flag):
        print("Could not add a message to the status bar.")
          
    opmode = vrep.simx_opmode_oneshot_wait
     
    vrep.simxStartSimulation(clientID, opmode)
    ret,shaft1=vrep.simxGetObjectHandle(clientID,"shaft_1",opmode)
    ret,shaft2=vrep.simxGetObjectHandle(clientID,"shaft_2",opmode)
    ret,shaft3=vrep.simxGetObjectHandle(clientID,"shaft_3",opmode)

    while True:
        if keyboard.is_pressed("q"):
            vrep.simxSetJointTargetVelocity(clientID,shaft1,0.15,opmode)
            print("w")
            
        if keyboard.is_pressed("w"):
            vrep.simxSetJointTargetVelocity(clientID,shaft1,0,opmode)
            print("s")
            
        if keyboard.is_pressed("a"):
            vrep.simxSetJointTargetVelocity(clientID,shaft2,0.5,opmode)
            print("a")

        if keyboard.is_pressed("s"):
            vrep.simxSetJointTargetVelocity(clientID,shaft2,0,opmode)
            print("b")
            
        if keyboard.is_pressed("z"):
            vrep.simxSetJointTargetVelocity(clientID,shaft3,0.3,opmode)
            print("c")

        if keyboard.is_pressed("x"):
            vrep.simxSetJointTargetVelocity(clientID,shaft3,0,opmode)
            print("z")            
else:
    print ('Failed connecting to  remote API server')
    print ('End')




Task 1 << Previous Next >> RoboDK

Copyright © All rights reserved | This template is made with by Colorlib